package net.simflow.swf.analyzer.bgnetwork.stats.opt;

import java.util.Collection;
import java.util.HashSet;
import java.util.TreeMap;

import net.simflow.swf.analyzer.bgnetwork.data.BGNetwork;
import net.simflow.swf.analyzer.bgnetwork.data.BGNode;

public class BGNetworkDegreeDistributionAnalyzer {

	private static BGNetworkDegreeDistributionAnalyzer instance = new BGNetworkDegreeDistributionAnalyzer();

	private BGNetworkDegreeDistributionAnalyzer() {
	}

	public static BGNetworkDegreeDistributionAnalyzer getInstance() {
		return instance;
	}

	public TreeMap<Integer, HashSet<BGNode>> analyzeFlowDegreeDistribution(
			BGNetwork network) {
		return analyzeDegreeDistribution(network.getFlowMap().values());

	}

	public TreeMap<Integer, HashSet<BGNode>> analyzeServiceDegreeDistribution(
			BGNetwork network) {
		return analyzeDegreeDistribution(network.getServiceMap().values());
	}

	public TreeMap<Integer, HashSet<BGNode>> analyzeDegreeDistribution(
			Collection<? extends BGNode> bgNetwork) {
		if (bgNetwork == null) {
			return null;
		}

		TreeMap<Integer, HashSet<BGNode>> map = new TreeMap<Integer, HashSet<BGNode>>();

		for (BGNode n : bgNetwork) {
			int d = n.getNeighborSet().size();
			HashSet<BGNode> set = map.get(d);
			if (set == null) {
				set = new HashSet<BGNode>();
				map.put(d, set);
			}
			set.add(n);
		}

		return map;
	}
}
